package com.woniuxy.yuekeuser.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.model.City;
import com.woniuxy.yuekeuser.service.ICityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class CityController {
    @Autowired
    private ICityService iCityService;

    @GetMapping("getCitys")//分页查询所有城市
    public Result getCitys(@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "10")int pageSize, @RequestParam(defaultValue = "")String cityName){
        log.info("分页查询全部城市的方法参数为{},{},{},{}",currPage,pageSize,cityName);
        if (cityName.equals("")){
            cityName=null;
        }
        Page<City> page= new Page<>();
        page.setCurrent(currPage);
        page.setSize(pageSize);
        IPage<City> cityList=iCityService.findAllCity(page,cityName);
        System.out.println(cityList.getRecords());
        return Result.success(cityList);
    }
    @GetMapping("getCityName")
    public Result getCityName(){
        log.info("执行查询所有城市的方法");
        List<City> allCitys = iCityService.findAllCitys();
        return Result.success(allCitys);
    }

    @PostMapping("addCityHeat")//添加城市的热度
    public Result addCityHeat(int cityId){
        log.info("执行点击城市页面增加城市热度的方法,参数为{}",cityId);
        City city = iCityService.queryCityByCityId(cityId);
        city.setCityHeat(city.getCityHeat()+1);
        //添加城市的热度
        iCityService.addCityHeat(city);
        return Result.success();
    }
}

